//
//  PeopleInfoViewController.m
//  Toyohu
//
//  Created by Heige on 16/4/21.
//  Copyright © 2016年 celia. All rights reserved.
//

#import "PeopleInfoViewController.h"
#import "SpaceStyleTwoTableViewCell.h"
#import "SureButtonTableViewCell.h"
#import "LoginViewController.h"
#import "FirstStyleTableViewCell.h"
#import "SencondTableViewCell.h"
#import "MyPersonalInfoViewController.h"
#import "GPCustomMD.h"


#import "IssueViewController.h"
#import "AnswerViewController.h"
#import "myCollectViewController.h"
#import "MyFocusOnViewController.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "LocalFileManager.h"

static NSString *SpaceStyleTwoTableViewCellReuseId = @"SpaceStyleTwoTableViewCell";
static NSString *SureButtonTableViewCellReuseId = @"SureButtonTableViewCell";
static NSString *FirstStyleTableViewCellReuseId = @"FirstStyleTableViewCell";
static NSString *SencondTableViewCellReuseId = @"SencondTableViewCell";

#define WIDTH self.view.bounds.size.width
#define HEIGHT self.view.bounds.size.height

@interface PeopleInfoViewController ()<UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate>
{
     UITableView *myTableView;
     UIAlertView *myAlertView;
}

@end

@implementation PeopleInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT-64) style:UITableViewStylePlain];
    myTableView.delegate = self;
    myTableView.dataSource = self;
    myTableView.showsVerticalScrollIndicator = NO;
    myTableView.showsHorizontalScrollIndicator = NO;
    myTableView.backgroundColor =[UIColor colorWithRed:231/255.0 green:231/255.0 blue:231/255.0 alpha:1];
    [self.view addSubview:myTableView];
    
    [myTableView registerNib:[UINib nibWithNibName:@"SpaceStyleTwoTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:SpaceStyleTwoTableViewCellReuseId];
    [myTableView registerNib:[UINib nibWithNibName:@"SureButtonTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:SureButtonTableViewCellReuseId];
    [myTableView registerNib:[UINib nibWithNibName:@"FirstStyleTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:FirstStyleTableViewCellReuseId];
    [myTableView registerNib:[UINib nibWithNibName:@"SencondTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:SencondTableViewCellReuseId];
    
    myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //导航栏左边项
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
    leftButton.frame = CGRectMake(0, 0, 10, 15);
    [leftButton setBackgroundImage:[UIImage imageNamed:@"ARROW---LEFT@2x"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(goBackPreviousPage) forControlEvents:UIControlEventTouchDown];
    
    UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftButtonItem;
    
    UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 30)];
    navLabel.textColor = [UIColor whiteColor];
    navLabel.backgroundColor = [UIColor clearColor];
    navLabel.textAlignment = NSTextAlignmentCenter;
    navLabel.text = @"个人信息";
    navLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:22];
    self.navigationItem.titleView = navLabel;
    
    UIView *View = [[UIView alloc]initWithFrame:CGRectZero];
    myTableView.tableFooterView= View;
    
    UIButton *quitButton = [UIButton buttonWithType:UIButtonTypeSystem];
    quitButton.frame = CGRectMake(0, HEIGHT-50, WIDTH, 50);
    [self.view addSubview:quitButton];
    [quitButton setTitle:@"退出当前账号" forState:UIControlStateNormal];
    [quitButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [quitButton addTarget:self action:@selector(LoginOut) forControlEvents:UIControlEventTouchUpInside];
    [quitButton setBackgroundColor:[UIColor whiteColor]];
    
    
    
}

-(void)goBackPreviousPage{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark --UITableViewDelegate  UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 6;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0) {
        myTableView.rowHeight=60;
        FirstStyleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FirstStyleTableViewCellReuseId];
//        [cell.headImage sd_setImageWithURL:[NSURL URLWithString:self.headUrlStr] placeholderImage:[UIImage imageNamed:@"head-portrait-small.png"]];
        if ([LocalFileManager getImageForKey:@"myUserImg"]) {
            cell.headImage.image = [LocalFileManager getImageForKey:@"myUserImg"];
        }else{
            cell.headImage.image = [UIImage imageNamed:@"图层-2"];
        }
        if (!cell) {
            cell = (FirstStyleTableViewCell *)[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier: FirstStyleTableViewCellReuseId];
        }
        cell.name.text = self.nameLabel;
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;
    }
    else if (indexPath.row==1)
    {
        myTableView.rowHeight=30;
        SpaceStyleTwoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SpaceStyleTwoTableViewCellReuseId];
        if (!cell)
        {
            cell = (SpaceStyleTwoTableViewCell *)[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier: SpaceStyleTwoTableViewCellReuseId];
        }
        
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;
    }
    else if (indexPath.row==2)
    {
        myTableView.rowHeight=56;
        SencondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SencondTableViewCellReuseId];
        if (!cell) {
            cell = (SencondTableViewCell *)[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier: SencondTableViewCellReuseId];
        }
        cell.name.text = @"我发布的";
        cell.img.image = [UIImage imageNamed:@"pencil"];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;
    }
    else if (indexPath.row==3)
    {
         myTableView.rowHeight=56;
        SencondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SencondTableViewCellReuseId];
        if (!cell) {
            cell = (SencondTableViewCell *)[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier: SencondTableViewCellReuseId];
        }
        cell.name.text = @"我回复的";
        cell.img.image = [UIImage imageNamed:@"massage-1"];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;
    }
    else if (indexPath.row==4)
    {
         myTableView.rowHeight=56;
        SencondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SencondTableViewCellReuseId];
        if (!cell) {
            cell = (SencondTableViewCell *)[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier: SencondTableViewCellReuseId];
        }
        cell.name.text = @"我收藏的";
        cell.img.image = [UIImage imageNamed:@"ribbon"];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;
    }
    else
    {
         myTableView.rowHeight=56;
        SencondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SencondTableViewCellReuseId];
        if (!cell) {
            cell = (SencondTableViewCell *)[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier: SencondTableViewCellReuseId];
        }
        cell.name.text = @"我关注的";
        cell.img.image = [UIImage imageNamed:@"focus-on"];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;
    }
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0) {
        MyPersonalInfoViewController *VC = [[MyPersonalInfoViewController alloc]init];
        VC.childrenInfo = self.childrenInfo;
        VC.headUrlStr = self.headUrlStr;
        VC.nameLabel = self.nameLabel;
        VC.phoneStr = self.phoneStr;
        WS
        VC.changeInfoBlock = ^(NSString *phoneText,NSString *babyNickName,NSString *dateOfBirthText,NSString *sexText){
            SS
            FirstStyleTableViewCell *cell = [myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
            cell.headImage.image = [LocalFileManager getImageForKey:@"myUserImg"];
            strongSelf.changeInfoBlock(phoneText,babyNickName,dateOfBirthText,sexText);
        };
        [self presentViewController:VC animated:YES completion:nil];
    }
    else if (indexPath.row==2)
    {
        IssueViewController*loginVc = [[IssueViewController alloc]init];
        [self.navigationController pushViewController:loginVc animated:YES];
    }
    else if (indexPath.row==3)
    {
        AnswerViewController*loginVc = [[AnswerViewController alloc]init];
        [self.navigationController pushViewController:loginVc animated:YES];
    }
    else if (indexPath.row==4)
    {
        myCollectViewController*loginVc = [[myCollectViewController alloc]init];
        [self.navigationController pushViewController:loginVc animated:YES];
    }
    else if (indexPath.row==5)
    {
        [[[UIApplication sharedApplication] keyWindow] setRootViewController:[LoginViewController new]];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"loginInfo"];
    }
}

-(void)LoginOut
{
    myAlertView = [[UIAlertView alloc]initWithTitle:@"你确定要退出么？" message:@"退出请慎重！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
    [myAlertView show];
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==0) {
        LoginViewController*loginVc = [[LoginViewController alloc]init];
        [self presentViewController:loginVc animated:YES completion:nil];
    }else{
        [myAlertView dismissWithClickedButtonIndex:1 animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
